package medium;

import java.util.ArrayList;
import java.util.List;

/**
 * 22. 括号生成
 * 创建时间：2024/6/21 下午3:53
 */
public class 括号生产_22 {
    List<String> ans = new ArrayList<>(); ;
    char[] path;
    int n;
    public List<String> generateParenthesis(int n) {
        path = new char[n * 2];
        this.n = n;
        dfs(0, 0);
        return ans;
    }

    public void dfs(int l, int r) {
        if (l == n && r == n) {
            ans.add(new String(path));
            return;
        }
        if (l < n) {
            path[l + r] = '(';
            dfs(l + 1, r);
        }
        if (r < l) {
            path[l + r] = ')';
            dfs(l, r + 1);
        }
    }

    public static void main(String[] args) {
        括号生产_22 a = new 括号生产_22();
        System.out.println(a.generateParenthesis(3));
    }

}
